Imports CapaDeDatos
Public Class nFactura
    Private Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Private Localidad As CapaDeNegocios.nLocalidad = New CapaDeNegocios.nLocalidad
    Private CategoriaFiscal As CapaDeNegocios.nCategoriaFiscal = New CapaDeNegocios.nCategoriaFiscal
    Private NotaDeCredito As CapaDeNegocios.nNotaDeCredito = New CapaDeNegocios.nNotaDeCredito
    Private NotaDeDebito As CapaDeNegocios.nNotaDeDebito = New CapaDeNegocios.nNotaDeDebito
    Private DetalleFactura As CapaDeNegocios.nDetalleFacturaVenta = New CapaDeNegocios.nDetalleFacturaVenta

    Private Factura As dFactura = New dFactura()
    Public Function Insertar(ByVal Numero As String, ByVal Subtotal As String, ByVal Total As String, ByVal IVA As String, ByVal CodigoCliente As String, ByVal CodigoCategoria As String, ByVal IdTalonario As String) As String
        Dim dts As DataSet
        dts = Factura.Insertar(Numero, Subtotal, Total, IVA, CodigoCliente, CodigoCategoria, IdTalonario)
        If dts.Tables(0).Rows.Count() > 0 Then
            Return dts.Tables(0).Rows(0)("IdFactura").ToString()
        Else
            Return ""
        End If
    End Function
    Public Function Eliminar(ByVal idFactura As String) As Boolean
        Return Factura.Eliminar(idFactura)
    End Function
    Public Function Modificar(ByVal idFactura As String, ByVal Numero As String, ByVal Fecha As String, ByVal Estados As String, ByVal Subtotal As String, ByVal Total As String, ByVal IVA As String, ByVal CodigoCliente As String, ByVal CodigoCategoria As String, ByVal IdTalonario As String) As Boolean
        Return Factura.Modificar(idFactura, Numero, Fecha, Estados, Subtotal, Total, IVA, CodigoCliente, CodigoCategoria, IdTalonario)
    End Function
    Public Function BuscarFiltrado(ByVal idFactura As String, ByVal Numero As String, ByVal Fecha As String, ByVal Estados As String, ByVal Subtotal As String, ByVal Total As String, ByVal IVA As String, ByVal CodigoCliente As String, ByVal CodigoCategoria As String, ByVal IdTalonario As String) As DataSet
        Dim dts As DataSet = Factura.BuscarFiltrado(idFactura, Numero, Fecha, Estados, Subtotal, Total, IVA, CodigoCliente, CodigoCategoria, IdTalonario)
        dts.Tables(0).Columns.Add("RazonSocial")
        dts.Tables(0).Columns.Add("detalle")
        dts.Tables(0).Columns.Add("NC")
        dts.Tables(0).Columns.Add("ND")
        Dim dtsNC As DataSet
        Dim dtsND As DataSet
        Dim Total2 As Double = 0
        Dim i As Integer
        For i = 0 To dts.Tables(0).Rows.Count - 1
            Try
                dts.Tables(0).Rows(i)("RazonSocial") = Cliente.Listar(dts.Tables(0).Rows(i)("CodigoCliente").ToString(), "", "", "", "", "", "", "", "", "", "", "").Tables(0).Rows(0)("RazonSocial").ToString()
            Catch ex As Exception
                dts.Tables(0).Rows(i)("RazonSocial") = "Error, Cliente no existe"
            End Try
            Try
                dts.Tables(0).Rows(i)("detalle") = CategoriaFiscal.Buscar(dts.Tables(0).Rows(i)("CodigoCategoria").ToString(), "").Tables(0).Rows(0)("Detalle").ToString()
            Catch ex As Exception
                dts.Tables(0).Rows(i)("detalle") = "Error, Categoria no existe"
            End Try
            Total2 = 0
            Try
                dtsNC = NotaDeCredito.BuscarFiltrado("", "", "", "", dts.Tables(0).Rows(i)("IdFactura").ToString(), "", "")
                For Each row As DataRow In dtsNC.Tables(0).Rows
                    If row("Estado").ToString() <> "ANULADA" Then
                        Total2 = Total2 + CDbl(row("Total").ToString())
                    End If
                Next
            Catch ex As Exception
                Total2 = 0
            End Try
            dts.Tables(0).Rows(i)("NC") = Total2
            Total2 = 0
            Try
                dtsND = NotaDeDebito.BuscarFiltrado("", "", "", "", dts.Tables(0).Rows(i)("IdFactura").ToString(), "", "")
                For Each row As DataRow In dtsND.Tables(0).Rows
                    If row("Estado").ToString() <> "ANULADA" Then
                        Total2 = Total2 + CDbl(row("Total").ToString())
                    End If
                Next
            Catch ex As Exception
                Total2 = 0
            End Try
            dts.Tables(0).Rows(i)("ND") = Total2

        Next
        Return dts
    End Function
    Public Function ObtenerNumero(ByVal CodigoCategoria As String) As Integer
        Dim dts As DataSet = Factura.ObtenerNumero(CodigoCategoria)
        If dts.Tables(0).Rows.Count() > 0 Then
            Return CInt(dts.Tables(0).Rows(0)(0).ToString()) + 1
        Else
            Return 1
        End If
    End Function
    Public Function Existe(ByVal Numero As String) As Boolean
        Return Existe(Numero)
    End Function
    Public Function PrepararImpresion(ByVal IdFactura As String) As DataSet
        Dim DtsFactura As DataSet = New DataSet()
        DtsFactura.Tables.Add("TblImpresionFactura")
        DtsFactura.Tables(0).Columns.Add("Dia")
        DtsFactura.Tables(0).Columns.Add("Mes")
        DtsFactura.Tables(0).Columns.Add("Ano")
        DtsFactura.Tables(0).Columns.Add("Nombre")
        DtsFactura.Tables(0).Columns.Add("Telefono")
        DtsFactura.Tables(0).Columns.Add("Domicilio")
        DtsFactura.Tables(0).Columns.Add("Localidad")
        DtsFactura.Tables(0).Columns.Add("CondicionAnteIVA")
        DtsFactura.Tables(0).Columns.Add("CUIT")
        DtsFactura.Tables(0).Columns.Add("Contado")
        DtsFactura.Tables(0).Columns.Add("ConsumidorFinal")
        DtsFactura.Tables(0).Columns.Add("Exento")
        DtsFactura.Tables(0).Columns.Add("Monotributo")
        DtsFactura.Tables(0).Columns.Add("ResponsableInscripto")
        DtsFactura.Tables(0).Columns.Add("CuentaCorriente")
        DtsFactura.Tables(0).Columns.Add("Cantidad")
        DtsFactura.Tables(0).Columns.Add("Descripcion")
        DtsFactura.Tables(0).Columns.Add("PrecioUnitario")
        DtsFactura.Tables(0).Columns.Add("PrecioTotal")
        DtsFactura.Tables(0).Columns.Add("SubTotal")
        DtsFactura.Tables(0).Columns.Add("IVA")
        DtsFactura.Tables(0).Columns.Add("Total")
        DtsFactura.Tables(0).Columns.Add("PorcentajeIVA")

        Dim DtsFacturaTemp As DataSet = BuscarFiltrado(IdFactura, "", "", "", "", "", "", "", "", "")
        Dim DtsClienteTemp As DataSet = Cliente.Listar(DtsFacturaTemp.Tables(0).Rows(0)("CodigoCliente").ToString, "", "", "", "", "", "", "", "", "", "", "")
        Dim DtsLocalidadTemp As DataSet = Localidad.Buscar(DtsClienteTemp.Tables(0).Rows(0)("CodigoLocalidad").ToString)
        Dim DtsDetalleFactura As DataSet = DetalleFactura.BuscarFiltrado("", "", "", "", IdFactura, "", "")

        For Each row As DataRow In DtsDetalleFactura.Tables(0).Rows
            Dim dr As DataRow
            dr = DtsFactura.Tables(0).NewRow()
            dr("Dia") = Convert.ToDateTime(DtsFacturaTemp.Tables(0).Rows(0)("Fecha")).Day
            dr("Mes") = Convert.ToDateTime(DtsFacturaTemp.Tables(0).Rows(0)("Fecha")).Month
            dr("Ano") = Convert.ToDateTime(DtsFacturaTemp.Tables(0).Rows(0)("Fecha")).Year
            dr("Nombre") = DtsClienteTemp.Tables(0).Rows(0)("RazonSocial").ToString
            dr("Telefono") = DtsClienteTemp.Tables(0).Rows(0)("TelCel").ToString
            dr("Domicilio") = DtsClienteTemp.Tables(0).Rows(0)("Direccion").ToString
            dr("CUIT") = DtsClienteTemp.Tables(0).Rows(0)("CUIT").ToString
            dr("Localidad") = DtsLocalidadTemp.Tables(0).Rows(0)("Nombre").ToString

            If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "1" Then
                dr("ResponsableInscripto") = "X"
                dr("PrecioUnitario") = row("Subtotal").ToString
                dr("PrecioTotal") = row("Subtotal").ToString
                dr("SubTotal") = DtsFacturaTemp.Tables(0).Rows(0)("SubTotal").ToString
                dr("IVA") = DtsFacturaTemp.Tables(0).Rows(0)("IVA").ToString

            Else
                dr("ResponsableInscripto") = ""
                dr("PrecioUnitario") = row("Total").ToString
                dr("PrecioTotal") = row("Total").ToString
                dr("SubTotal") = ""
                dr("IVA") = ""
            End If
            If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "2" Then
                dr("Monotributo") = "X"
            Else
                dr("Monotributo") = ""
            End If
            If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "3" Then
                dr("ConsumidorFinal") = "X"
            Else
                dr("ConsumidorFinal") = ""
            End If
            If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "4" Then
                dr("Exento") = "X"
            Else
                dr("Exento") = ""
            End If
            dr("CondicionAnteIVA") = ""
            dr("Contado") = ""
            dr("CuentaCorriente") = "X"

            dr("Cantidad") = "1"
            'if (row("Concepto").ToString=)
            If row("CodigoRemito").ToString = "" Then
                dr("Descripcion") = row("Concepto").ToString
            Else
                dr("Descripcion") = "Remito Numero : " + row("Concepto").ToString

            End If
            dr("Total") = DtsFacturaTemp.Tables(0).Rows(0)("Total").ToString
            If DtsClienteTemp.Tables(0).Rows(0)("CodigoCategoria").ToString = "1" Then
                dr("PorcentajeIVA") = Math.Round(((CDbl(DtsFacturaTemp.Tables(0).Rows(0)("Total").ToString) / CDbl(DtsFacturaTemp.Tables(0).Rows(0)("SubTotal").ToString)) - 1) * 100, 2).ToString
            Else
                dr("PorcentajeIVA") = ""
            End If
            DtsFactura.Tables(0).Rows.Add(dr)
        Next
        Return DtsFactura
    End Function

End Class
